package com.zjhcsoft.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjhcsoft.auth.dao.AppResourceMapper;
import com.zjhcsoft.auth.dao.ChannelMapper;
import com.zjhcsoft.auth.dao.CorporationMapper;
import com.zjhcsoft.auth.entity.AppResource;
import com.zjhcsoft.auth.entity.Channel;
import com.zjhcsoft.auth.entity.Corporation;
import com.zjhcsoft.auth.service.AppResourceService;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 业务系统信息表 服务实现类
 * </p>
 *
 * @author wzya
 * @since 2021-03-26
 */
@Service
public class AppResourceServiceImpl extends ServiceImpl<AppResourceMapper, AppResource> implements AppResourceService {

    @Resource
    ChannelMapper channelMapper;
    @Resource
    CorporationMapper corporationMapper;


    @Override
    public List<AppResource> getAppResourceByChannelCodesAndCorporationCode(List<String> channelCodes, String corporationCode) {

        QueryWrapper<Channel> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(Channel::getChannelCode,channelCodes);
        List<Channel> channels = channelMapper.selectList(wrapper);
//        List<Long> channelIds = channelService.lambdaQuery().in(Channel::getChannelCode,channelCodes).list().stream().map(channel -> channel.getId()).collect(Collectors.toList());
        QueryWrapper<Corporation> corporationQueryWrapper = new QueryWrapper<>();
        corporationQueryWrapper.lambda().eq(Corporation::getCorporationCode,corporationCode);
        Corporation corporation = corporationMapper.selectOne(corporationQueryWrapper);
        if(null!=channels && null!= corporation){
            List<Long> channelIds = channels.stream().map(channel -> channel.getId()).collect(Collectors.toList());
            Long corporationId = corporation.getId();
            return this.lambdaQuery().in(AppResource::getChannelId,channelIds).eq(AppResource::getCorporationId,corporationId).list();
        }else {
           return null;
        }
    }

    @Override
    public AppResource getAppResourceByChannelCodeAndCorporationCode(String channelCode, String corporationCode) {

        QueryWrapper<Channel> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Channel::getChannelCode,channelCode);
        Channel channel = channelMapper.selectOne(wrapper);

        QueryWrapper<Corporation> corporationQueryWrapper = new QueryWrapper<>();
        corporationQueryWrapper.lambda().eq(Corporation::getCorporationCode,corporationCode);
        Corporation corporation = corporationMapper.selectOne(corporationQueryWrapper);
        if(null!=channel && null!= corporation){
            Long channelId = channel.getId();
            Long corporationId = corporation.getId();
            return this.lambdaQuery().eq(AppResource::getChannelId,channelId).eq(AppResource::getCorporationId,corporationId).one();
        }else {
            return null;
        }
    }
}
